package command
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import event.TripManagerEvent;
	
	import flash.profiler.showRedrawRegions;
	
	import model.Model;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import services.databasewrapper.DatabaseWrapper;
	
	import valueObjects.VOTrip;
	
	public class SaveTripCommand implements Command, IResponder
	{
	
		
		public function SaveTripCommand()
		{
		}
		
		public function execute(event:CairngormEvent):void
		{
			var trip:VOTrip = (event as TripManagerEvent).additionalData.trip;
			// creating a new database Wrapper
			var databaseWrapper:DatabaseWrapper = new DatabaseWrapper();
			databaseWrapper.addEventListener(FaultEvent.FAULT, fault);
			databaseWrapper.addEventListener(ResultEvent.RESULT, result);
			databaseWrapper.save_Trip(trip);
		}
		
		public function result(data:Object):void
		{
			if (data.result != null)
			{	
				
				Model.instance.currentTrip.tripData = data.result;
				trace('updating model');
			}	
			else
				fault(null);
			
		}
		
		public function fault(info:Object):void
		{
			Alert.show("Connection error!");
		}
	}
}